package app;


import com.opensymphony.xwork2.ActionSupport;
import java.io.PrintWriter;
import java.util.HashMap;
import javawebparts.request.RequestHelpers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;


/**
 * This is a simple test Action that handles all AJAX requests from the client.
 *
 * @author <a href="mailto:fzammetti@omnytex.com">Frank W. Zammetti</a>
 */
public class TestAction extends ActionSupport implements
  ServletResponseAware, ServletRequestAware {


  /**
   * Request object.
   */
  private HttpServletRequest request;


  /**
   * Response object.
   */
  private HttpServletResponse response;


  /**
   * Setter for response field.
   *
   * @param response The Response object to store.
   */
  public void setServletResponse(final HttpServletResponse inResponse) {

    this.response = inResponse;

  } // End setServletResponse().


  /**
   * Getter for the response field.
   *
   * @return The stored Response object.
   */
  public HttpServletResponse getServletResponse() {

    return response;

  } // End getServletResponse().


  /**
   * Setter for request field.
   *
   * @param request The Request object to store.
   */
  public void setServletRequest(final HttpServletRequest inRequest) {

    this.request = inRequest;

  } // End setServletRequest().


  /**
   * Getter for the request field.
   *
   * @return The stored Request object.
   */
  public HttpServletRequest getServletRequest(){

    return request;

  } // End getServletRequest().


  /**
   * Called to execute test #1.
   *
   * @return The response to return to the client.
   */
  public String test1() throws Exception {

    PrintWriter out = response.getWriter();
    out.write("I am the response returned by the server for test #1");
    return null;

  } // End test1().


  /**
   * Called to execute test #2.
   *
   * @return The response to return to the client.
   */
  public String test2() throws Exception {

    PrintWriter out = response.getWriter();
    out.write("I am the response returned by the server for test #2");
    return null;

  } // End test2().


  /**
   * Called to execute test #3 (parameters).
   *
   * @return The response to return to the client.
   */
  public String test3Parameters() throws Exception {

    HashMap<String, String> formValues = new HashMap<String, String>();
    formValues.put("firstName", request.getParameter("firstName"));
    formValues.put("lastName", request.getParameter("lastName"));
    formValues.put("attractiveness", request.getParameter("attractiveness"));
    formValues.put("marryingMaterial",
      request.getParameter("marryingMaterial"));
    formValues.put("neverHappen", request.getParameter("neverHappen"));
    createSentence("Parameters", formValues);
    return null;

  } // End test3Parameters().


  /**
   * Called to execute test #3 (JSON).
   * Note that the method of parsing the incoming JSON is in no way, shape or
   * form condoned!  This is simply a quick and dirty way to make this work.
   *
   * @return The response to return to the client.
   */
  public String test3JSON() throws Exception {

    HashMap<String, String> formValues = new HashMap<String, String>();
    String bodyContent = RequestHelpers.getBodyContent(request);
    int start = 0;
    int end = 0;
    start = bodyContent.indexOf("\"firstName\"");
    end = bodyContent.indexOf(",", start);
    formValues.put("firstName", bodyContent.substring(start + 15, end - 1));
    start = bodyContent.indexOf("\"lastName\"");
    end = bodyContent.indexOf(",", start);
    formValues.put("lastName", bodyContent.substring(start + 14, end - 1));
    start = bodyContent.indexOf("\"attractiveness\"");
    end = bodyContent.indexOf(",", start);
    formValues.put("attractiveness", bodyContent.substring(start + 20,
      end - 1));
    start = bodyContent.indexOf("\"marryingMaterial\"");
    end = bodyContent.indexOf(",", start);
    formValues.put("marryingMaterial", bodyContent.substring(start + 22,
      end - 1));
    start = bodyContent.indexOf("\"neverHappen\"");
    end = bodyContent.indexOf("}", start);
    formValues.put("neverHappen", bodyContent.substring(start + 17, end - 2));
    createSentence("JSON", formValues);
    return null;

  } // End test3JSON().


  /**
   * Called to execute test #3 (XML).
   *
   * Note that the method of parsing the incoming XML is in no way, shape or
   * form condoned!  This is simply a quick and dirty way to make this work.
   *
   * @return The response to return to the client.
   */
  public String test3XML() throws Exception {

    HashMap<String, String> formValues = new HashMap<String, String>();
    String bodyContent = RequestHelpers.getBodyContent(request);
    int start = 0;
    int end = 0;
    start = bodyContent.indexOf("<firstName>");
    end = bodyContent.indexOf("</firstName>");
    formValues.put("firstName", bodyContent.substring(start + 11, end));
    start = bodyContent.indexOf("<lastName>");
    end = bodyContent.indexOf("</lastName>");
    formValues.put("lastName", bodyContent.substring(start + 10, end));
    start = bodyContent.indexOf("<attractiveness>");
    end = bodyContent.indexOf("</attractiveness>");
    formValues.put("attractiveness", bodyContent.substring(start + 16, end));
    start = bodyContent.indexOf("<marryingMaterial>");
    end = bodyContent.indexOf("</marryingMaterial>");
    formValues.put("marryingMaterial", bodyContent.substring(start + 18, end));
    start = bodyContent.indexOf("<neverHappen>");
    end = bodyContent.indexOf("</neverHappen>");
    formValues.put("neverHappen", bodyContent.substring(start + 13, end));
    createSentence("XML", formValues);
    return null;

  } // End test3XML().


  /**
   * Called to create a sentence from the parsed form values for test #3.
   *
   * @param inMethod     The method the values were retrieved from, "XML",
   *                     "JSON" or "Parameters".
   * @param inFormValues The form values parsed from the request.
   */
  private void createSentence(final String inMethod,
    final HashMap<String, String> inFormValues) throws Exception {

    if (((String)inFormValues.get("neverHappen")).equalsIgnoreCase("true")) {
      inFormValues.put("neverHappen", "are at least realistic but still");
    } else {
      inFormValues.put("neverHappen", "are delusional and");
    }
    PrintWriter out = response.getWriter();
    out.print("Test #3 via " + inMethod + ":\n\n");
    out.print("It is a known fact that " + inFormValues.get("firstName") +
      " " + inFormValues.get("lastName") + " is " +
      inFormValues.get("attractiveness") + " and " +
      inFormValues.get("marryingMaterial") + " marrying material.  " +
      "Regardless, it is just as well-known a fact that you " +
      inFormValues.get("neverHappen") + " do not have any sort of chance in " +
      "this life, or any other, so just deal with it!"
    );

  } // End createSentence().


} // End class.
